/*
 * Events.c
 *      Author: DePhille
 */



/**
 * Authenticates the user.
 *
 * See GSourceFunc
 */
static gboolean	PC_Event_Login(gpointer data) {
	// Check input:
	if (data) {
		purple_debug_warning("PidginCote", "PC_Event_Login: Parameter 'data' is not empty. Memory leak?\r\n");
	}

	// Fetch information and log in:
	PC_Net_Login(purple_prefs_get_string("/plugins/gtk/gtk-dephille-pidgincote/username"), purple_prefs_get_string("/plugins/gtk/gtk-dephille-pidgincote/password"));

	// Do not loop this event:
	return FALSE;
}


/**
 * Scans the logfiles for all active accounts.
 *
 * See GSourceFunc
 */
static gboolean	PC_Event_ScanActiveAccounts(gpointer data) {
	GList*					curNode;	// Clean-up: Yes
	const PurpleAccount*	curAccount;	// Clean-up: No

	// Check input:
	if (data) {
		purple_debug_warning("PidginCote", "PC_Event_ScanActiveAccounts: Parameter 'data' is not empty. Memory leak?\r\n");
	}

	// Loop trough active accounts:
	curNode = purple_accounts_get_all_active();
	while (curNode) {
		// Cast:
		curAccount = (const PurpleAccount*) curNode->data;
		if (!curAccount) {
			purple_debug_warning("PidginCote", "PC_Event_ScanActiveAccounts: Found invalid PurpleAccount in list.\r\n");
		} else {
			// Scan account:
			PC_Sync_FS(curAccount);
		}

		// Next node:
		curNode = g_list_next(curNode);
	}

	// Loop this event:
	return TRUE;
}


/**
 * This event synchronizes the local database with the server's database by downloading all new logs from the server.
 *
 * See GSourceFunc
 */
static gboolean	PC_Event_SyncLogList(gpointer data) {
	GList*					curNode;	// Clean-up: Yes
	const PurpleAccount*	curAccount;	// Clean-up: No

	// Check input:
	if (data) {
		purple_debug_warning("PidginCote", "PC_Event_SyncLogList: Parameter 'data' is not empty. Memory leak?\r\n");
	}

	// Loop trough active accounts:
	curNode = purple_accounts_get_all_active();
	while (curNode) {
		// Cast:
		curAccount = (const PurpleAccount*) curNode->data;
		if (!curAccount) {
			purple_debug_warning("PidginCote", "PC_Event_SyncLogList: Found invalid PurpleAccount in list.\r\n");
		} else {
			// Synchronize account:
			PC_Sync_FullDB(curAccount);
		}

		// Next node:
		curNode = g_list_next(curNode);
	}

	// Clean-up:
	g_list_free(g_list_first(curNode));
	curNode = NULL;

	// Loop this event:
	return TRUE;
}
